/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;

public class guiProject2 extends JFrame implements ActionListener
{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;

private JButton open = new JButton ("Open");
private JButton add = new JButton ("ADD ITEM");
private JButton save = new JButton ("SAVE CHANGES");
private JButton remove = new JButton ("REMOVE ITEM");
private JButton display = new JButton ("DISPLAY ALL");
private JButton modify = new JButton ("MODIFY ITEM");
private JButton exit = new JButton ("EXIT");
private JLabel search = new JLabel ("Search Item");
private JFileChooser fileChooser = new JFileChooser();

private JLabel header = new JLabel ("CS 203 Project 2: Sharmin Akhter");
//private JScrollPane scroll = new JScrollPane(table);


private String column[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
private String row[][]= {{"", "", "", "", "", "", "", "", "", ""}};
//private String columnH="", bodyR="";
private String selection[] = {"Test1", "Test2", "Test3", "Test4"};
//
private JTable table = new JTable(row, column);
private JComboBox searchItem = new JComboBox(selection);
private JScrollPane scroll = new JScrollPane(table);


// layout = new GridBagLayout();
//constraints = new GridBagConstraints();

// container = getContentPane();

public guiProject2()
{

setTitle("Project 2");
container = getContentPane();
layout = new GridBagLayout();

container.setLayout(layout);
constraints = new GridBagConstraints();


constraints.weightx = 1000;
constraints.anchor = GridBagConstraints.CENTER;
addComponent(header, 0, 1, 2, 1);

constraints.anchor = GridBagConstraints.EAST;
addComponent(search, 0, 2, 2, 1);

constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(open, 1, 0, 1, 1);
addComponent(searchItem, 1, 2, 2, 1);
addComponent(add, 2, 0, 1, 1);
addComponent(remove, 2, 1, 1, 1);
addComponent(modify, 2, 2, 1, 1);
addComponent(exit, 2, 3, 1, 1);
addComponent(save, 3, 0, 1, 1);
addComponent(display, 3, 1, 1, 1);
fileChooser.setCurrentDirectory(new File("."));
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 1000;
addComponent(scroll, 5, 0, 4, 5);

open.addActionListener(this);
searchItem.addActionListener(this);
add.addActionListener(this);
remove.addActionListener(this);
modify.addActionListener(this);
exit.addActionListener(this);
save.addActionListener(this);
display.addActionListener(this);

setSize(450, 350);
setLocation(120, 30);
setVisible(true);
}//end constructor

public void addComponent(Component component, int row, int column, int width, int height)
{
//set gridx and gridy
constraints.gridx = column;
constraints.gridy = row;

//set gridwidth abd gridheight
constraints.gridwidth = width;
constraints.gridheight = height;

//set constraints and add component
layout.setConstraints(component, constraints);
//panelOne.add(component);
container.add(component);
}//end method addComponent





public void actionPerformed (ActionEvent event)
{
String actionCommand = event.getActionCommand();
if (event.getSource() instanceof JButton)
{
if ("Open".equals(actionCommand))
toOpen();
else if ("ADD ITEM".equals(actionCommand))
toAdd();
else if ("SAVE CHANGES".equals(actionCommand))
toSave();
else if ("REMOVE ITEM".equals(actionCommand))
toRemove();
else if ("DISPLAY ALL".equals(actionCommand))
toDisplay();
else if ("MODIFY ITEM".equals(actionCommand))
toModify();
else if ("EXIT".equals(actionCommand))
System.exit(0);
}
}//end method actionPerformed

private void toOpen()
{
if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
toOpen(fileChooser.getSelectedFile());
}//emd method toOpen

private void toOpen(File file)
{
try
{
BufferedReader buf = new BufferedReader(new FileReader(file));
String st = buf.readLine();
StringTokenizer stToken = new StringTokenizer(st, ",");
int columnNum = stToken.countTokens();
column = new String[columnNum];

for(int i= 0;i<columnNum;i++)
{
column[i]=stToken.nextToken();
System.out.println(column[i]);
}
String line = "";
int count = 0;
while ( (line = buf.readLine()) != null)
{
count++;
}
BufferedReader bff = new BufferedReader( new FileReader( file ) );
bff.readLine();

row = new String[count][columnNum];

for(int j= 0;j<count;j++)
{
line = bff.readLine();
StringTokenizer Token = new StringTokenizer(line, ",");
for(int l= 0;l<columnNum;l++)
{
row[j][l] = Token.nextToken();
System.out.println(row[j][l]);
}
}




//table = new JTable(row, column);
scroll.remove(this);
scroll= new JScrollPane( table );
buf.close();
bff.close();
}
catch (Exception e)
{
System.out.println(e);
}


}


private void toAdd()
{


}//end method toAdd

private void toSave()
{


}//end method toSave

private void toRemove()
{


}//end method toRemove

private void toDisplay()
{


}// end method toDisplay

private void toModify()
{


}// end method toModify



       public static void main(String[] args)
   {
       //Создание фрейма в потоке обработки событий
       SwingUtilities.invokeLater
               (
               new Runnable()
                  {
                  public void run()
                    {
                     new guiProject2();
                    }
                  }
               );
   }

}